package ssm.controller;

import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ssm.dao.*;
import ssm.service.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/track")
public class TrackController {

    @Autowired
    private TrackCarService trackCarService;

    @Autowired
    private TrackCardService trackCardService;

    @Autowired
    private TrackWifiService trackWifiService;

    @Autowired
    private TrackFaceService trackFaceService;

    @Autowired
    private TrackService trackService;



    String reg="/(\\d{4}-\\d{2}-\\d{2}\\d{2}:\\d{2}:\\d{2})|(\\d{2}-\\d{2}\\d{2}:\\d{2}:\\d{2})|(\\d{2}:\\d{2}:\\d{2})/";


    @RequestMapping("/getIcard")
    @ResponseBody
    public MyResult getIcard(
            @RequestParam List<String> list,
            @RequestParam  (value = "jid",required = false)String jid,
            @RequestParam  String starttime,
            @RequestParam String endtime,
            @RequestParam Integer page,
            @RequestParam Integer pageNum,
            @RequestParam Integer lx,
            @RequestParam(value = "name",required = false) String name){
        Map<String,Object>param=new HashMap<String,Object>();
        if(!list.isEmpty()){
            param.put("list",list);
        }
        if(name!=null&&!name.equals("")){
            param.put("name",name);
        }
        if(jid!=null&&!jid.equals("")){
            param.put("jid",jid);
        }


        param.put("starttime",starttime);
        param.put("endtime",endtime);
        if(page!=-1&&pageNum!=0){
            int pageStart=page*pageNum;
            param.put("pageStart",pageStart);
            param.put("pageEnd",pageNum);
        }
        if(StringUtils.isNullOrEmpty(jid)&&StringUtils.isNullOrEmpty(name)){
            if(lx!=null&&!lx.equals("")){
                param.put("lx",lx);
            }
        }

        System.out.println(param);
        Map<String,Object> result=trackService.getResult_bs(param);
//        System.out.println(result);
        return MyResult.ok(result);
    }


    @RequestMapping("/getWifi")
    @ResponseBody
    public MyResult getWifi(
                            @RequestParam List<String> list,
                            @RequestParam (value = "jid",required = false)String jid,
                            @RequestParam String starttime,
                            @RequestParam String endtime,
                            @RequestParam Integer page,
                            @RequestParam Integer pageNum,
                            @RequestParam Integer lx,
                            @RequestParam(value = "name",required = false)String name){
        Map<String,Object> result = trackWifiService.getByTimeOrJid(list,jid,starttime,endtime,page,pageNum,lx,name);
        return MyResult.ok(result);
    }


    @RequestMapping("/getBayonet")
    @ResponseBody
    public MyResult getBayonet(@RequestParam List<String> list,
                               @RequestParam (value = "cph",required = false)String cph,
                               @RequestParam  String starttime,
                               @RequestParam String endtime,
                               @RequestParam int page,
                               @RequestParam int pageNum,
                               @RequestParam Integer type,
                               @RequestParam(value = "name",required = false) String name,
                               @RequestParam (value = "jid",required = false)String jid){
        Map<String,Object> result = trackCarService.getByTimeOrJid(list,cph,starttime,endtime,page,pageNum,type,name,jid);
        return MyResult.ok(result);
    }

    @RequestMapping("/getFace")
    @ResponseBody
    public MyResult getFace(@RequestParam List<String> list,
                            @RequestParam (value = "jid",required = false)String jid,
                            @RequestParam  String starttime,
                            @RequestParam String endtime,
                            @RequestParam Integer page,
                            @RequestParam Integer pageNum,
                            @RequestParam Integer type,
                            @RequestParam(value = "lx",required = false)Integer lx,
                            @RequestParam(value = "name",required = false)String name){
        if(!StringUtils.isNullOrEmpty(jid)||!StringUtils.isNullOrEmpty(name)){
            Map<String,Object> result = trackFaceService.getByTimeOrJid(list,jid,starttime,endtime,page,pageNum,name);
            return MyResult.ok(result);
        }
        if(type==1){
            Map<String,Object> result = trackFaceService.getByTimeOrJid(list,jid,starttime, endtime,page,pageNum,lx,name);
            return MyResult.ok(result);
        }else if (type==0){
            Map<String,Object> result=trackFaceService.getOutSchoolByFace(list,starttime,endtime,page,pageNum);
            return MyResult.ok(result);
        }
        return MyResult.except("type传参错误,目前只支持0-校外,1-校内人员","传参错误");

    }


    @RequestMapping("/getCardDetail")//未完
    @ResponseBody
    public MyResult getCardDetail(  @RequestParam String lsh,
                                    @RequestParam String ly){
        List<Map<String,Object>> list=trackService.getByTable(lsh,ly);
        return MyResult.ok(list);
    }


    @RequestMapping("/getWifiByLsh")
    @ResponseBody
    public MyResult getWifiByLsh(@RequestParam String lsh){
        TrackWifi wifi=trackWifiService.getWifiByLsh(lsh);
        return MyResult.ok(wifi);
    }

    @RequestMapping("/getBigPicUrlByFace")
    @ResponseBody
    public MyResult getBigPicUrlByFace(@RequestParam(value = "faceUrl") String faceUrl){
        String bigPicUrl=trackService.getBigPicUrlByFace(faceUrl);
        return MyResult.ok(bigPicUrl);
    }






}
